 package server.events;

 import client.MapleCharacter;
 import constants.MapConstants;
 import handling.channel.ChannelServer;
 import handling.world.WorldBroadcastService;
 import server.MapleInventoryManipulator;
 import server.RandomRewards;
 import server.Timer;
 import server.maps.FieldLimitType;
 import server.maps.MapleMap;
 import server.maps.SavedLocationType;
 import tools.DateUtil;
 import tools.MaplePacketCreator;
 import tools.Randomizer;
 import tools.StringUtil;

 public abstract class MapleEvent
 {
   protected final MapleEventType type;
   protected final int channel;
/*  22 */   protected int playerCount = 0;
   protected boolean isRunning = false;

   public MapleEvent(int channel, MapleEventType type) {
/*  26 */     this.channel = channel;
/*  27 */     this.type = type;
   }

   public static void givePrize(MapleCharacter chr) {
/*  31 */     int reward = RandomRewards.getEventReward();
/*  32 */     if (reward == 0) {
/*  33 */       int mes = Randomizer.nextInt(900000) + 100000;
/*  34 */       chr.gainMeso(mes, true, false);
/*  35 */       chr.dropMessage(5, "你获得了 " + mes + " 金币.");
/*  36 */     } else if (reward == 1) {
/*  37 */       int cs = Randomizer.nextInt(50) + 50;
/*  38 */       chr.modifyCSPoints(1, cs, true);
/*  39 */       chr.dropMessage(5, "你获得了 " + cs + " 点点卷.");
/*  40 */     } else if (reward == 2) {
/*  41 */       int fe = Randomizer.nextInt(5) + 1;
/*  42 */       chr.addFame(fe);
/*  43 */       chr.dropMessage(5, "你获得了 " + fe + " 点人气.");
/*  44 */     } else if (reward == 3) {
/*  45 */       chr.dropMessage(5, "你悲剧了，什么也没得到.");
     } else {
/*  47 */       int max_quantity = 1;
/*  48 */       switch (reward) {
         case 5062000:
/*  50 */           max_quantity = 1;
           break;
         case 5220040:
/*  53 */           max_quantity = 1;
           break;
         case 5062002:
/*  56 */           max_quantity = 1;
           break;
       }
/*  59 */       int quantity = ((max_quantity > 1) ? Randomizer.nextInt(max_quantity) : 0) + 1;
/*  60 */       if (MapleInventoryManipulator.checkSpace(chr.getClient(), reward, quantity, "")) {
/*  61 */         MapleInventoryManipulator.addById(chr.getClient(), reward, (short)quantity, "活动获得 " + DateUtil.getNowTime());
       } else {
/*  63 */         givePrize(chr);
       }
     }
   }

   public static void setEvent(ChannelServer cserv, boolean auto) {
/*  69 */     if (auto && cserv.getEvent() > -1) {
/*  70 */       for (MapleEventType t : MapleEventType.values()) {
/*  71 */         MapleEvent e = cserv.getEvent(t);
/*  72 */         if (e.isRunning) {
/*  73 */           for (int i : e.type.mapids) {
/*  74 */             if (cserv.getEvent() == i) {
/*  75 */               WorldBroadcastService.getInstance().broadcastMessage(MaplePacketCreator.serverNotice(0, "活动入口已关闭!"));
/*  76 */               e.broadcast(MaplePacketCreator.serverNotice(0, " 活动将在30秒后开始!"));
/*  77 */               e.broadcast(MaplePacketCreator.getClock(30));
/*  78 */               Timer.EventTimer.getInstance().schedule(e::startEvent, 30000L);
               break;
             }
           }
         }
       }
     }
/*  85 */     cserv.setEvent(-1);
   }

   public static void mapLoad(MapleCharacter chr, int channel) {
/*  89 */     if (chr == null) {
       return;
     }
/*  92 */     for (MapleEventType t : MapleEventType.values()) {
/*  93 */       MapleEvent e = ChannelServer.getInstance(channel).getEvent(t);
/*  94 */       if (e.isRunning) {
/*  95 */         if (chr.getMapId() == 109050000) {
/*  96 */           e.finished(chr);
         }
/*  98 */         for (int i = 0; i < e.type.mapids.length; i++) {
/*  99 */           if (chr.getMapId() == e.type.mapids[i]) {
/* 100 */             e.onMapLoad(chr);
/* 101 */             if (i == 0) {
/* 102 */               e.incrementPlayerCount();
             }
           }
         }
       }
     }
   }

   public static void onStartEvent(MapleCharacter chr) {
/* 111 */     for (MapleEventType t : MapleEventType.values()) {
/* 112 */       MapleEvent e = chr.getClient().getChannelServer().getEvent(t);
/* 113 */       if (e.isRunning) {
/* 114 */         for (int i : e.type.mapids) {
/* 115 */           if (chr.getMapId() == i) {
/* 116 */             e.startEvent();
/* 117 */             setEvent(chr.getClient().getChannelServer(), false);
/* 118 */             chr.dropMessage(5, String.valueOf(t.desc) + " 已经开始了!");
           }
         }
       }
     }
   }

   public static String scheduleEvent(MapleEventType event, ChannelServer cserv) {
/* 126 */     if (cserv.getEvent() != -1 || cserv.getEvent(event) == null) {
/* 127 */       return "[提示] 活动不能进行设置,活动已经进行.";
     }
/* 129 */     for (int i : (cserv.getEvent(event)).type.mapids) {
/* 130 */       if (cserv.getMapFactory().getMap(i).getCharactersSize() > 0) {
/* 131 */         return "[提示] 活动已经开始或者活动地图有玩家存在！";
       }
     }
/* 134 */     cserv.setEvent((cserv.getEvent(event)).type.mapids[0]);
/* 135 */     cserv.getEvent(event).reset();
/* 136 */     WorldBroadcastService.getInstance().broadcastMessage(MaplePacketCreator.serverNotice(0, "Hello " + cserv.getServerName() + "! " + StringUtil.makeEnumHumanReadable(event.desc) + "活动在 " + cserv.getChannel() + "频道开始了，快来参加吧！"));
/* 137 */     return "";
   }

   public void incrementPlayerCount() {
/* 141 */     this.playerCount++;
/* 142 */     if (this.playerCount == 250) {
/* 143 */       setEvent(ChannelServer.getInstance(this.channel), true);
     }
   }

   public MapleEventType getType() {
/* 148 */     return this.type;
   }

   public boolean isRunning() {
/* 152 */     return this.isRunning;
   }

   public MapleMap getMap(int i) {
/* 156 */     return getChannelServer().getMapFactory().getMap(this.type.mapids[i]);
   }

   public ChannelServer getChannelServer() {
/* 160 */     return ChannelServer.getInstance(this.channel);
   }

   public void broadcast(byte[] packet) {
/* 164 */     for (int i = 0; i < this.type.mapids.length; i++) {
/* 165 */       getMap(i).broadcastMessage(packet);
     }
   }

   public abstract void finished(MapleCharacter paramMapleCharacter);

   public abstract void startEvent();

   public void onMapLoad(MapleCharacter chr) {
/* 174 */     if (MapConstants.isEventMap(chr.getMapId()) && FieldLimitType.Event.check(chr.getMap().getFieldLimit()) && FieldLimitType.Event2.check(chr.getMap().getFieldLimit())) {
/* 175 */       chr.send(MaplePacketCreator.showEventInstructions());
     }
   }

   public void warpBack(MapleCharacter chr) {
/* 180 */     int map = chr.getSavedLocation(SavedLocationType.EVENT);
/* 181 */     if (map <= -1) {
/* 182 */       map = 104000000;
     }
/* 184 */     MapleMap mapp = chr.getClient().getChannelServer().getMapFactory().getMap(map);
/* 185 */     chr.changeMap(mapp, mapp.getPortal(0));
   }

   public void reset() {
/* 189 */     this.isRunning = true;
/* 190 */     this.playerCount = 0;
   }

   public void unreset() {
/* 194 */     this.isRunning = false;
/* 195 */     this.playerCount = 0;
   }
 }


/* Location:              C:\PGX\Server.jar!\server\events\MapleEvent.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */